﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using RemoteController;

using System.Windows.Media.Imaging;

namespace VisualRemoteController
{
    public class GUICommandDecorator : CommandDecorator
    {
        GUICommandDecoratorStrategy GUIStrategy = null;

        #region decorator image retrieval algorithm simulation

        static Dictionary<String, BitmapImage> ExecuteBitmapImageDictionary
            = new Dictionary<string, BitmapImage>();
        static Dictionary<String, BitmapImage> UndoBitmapImageDictionary
            = new Dictionary<string, BitmapImage>();

        ///static constructor
        static GUICommandDecorator()
        {
            ///initializate the dictionaries of bitmap images that help simulate the retrieval of a
            ///picture of an object after a command has been invoked...
            ///

            BitmapImage ceilingFanOnBitmapImage = getBitmapImageForGif("ceilingfanon.gif");
            BitmapImage tvOnBitmapImage = getBitmapImageForGif("tvon.gif");
            BitmapImage stereoOnBitmapImage = getBitmapImageForGif("stereoon.gif");
            BitmapImage hottubOnBitmapImage = getBitmapImageForGif("hottubon.gif");
            BitmapImage openGarageBitmapImage = getBitmapImageForGif("opengarage.gif");
            BitmapImage lightBulbOnBitmapImage = getBitmapImageForGif("lightbulbon.gif");
            BitmapImage livingRoomLightsOnBitmapImage = getBitmapImageForGif("livingroomlightson.gif");
            BitmapImage bathroomLightsOnBitmapImage = getBitmapImageForGif("bathroomlightson.gif");
            BitmapImage dvdOnBitmapImage = getBitmapImageForGif("dvdon.gif");
            BitmapImage bathroomstereoOnBitmapImage = getBitmapImageForGif("stereoon.gif");


            BitmapImage ceilingFanOffBitmapImage = getBitmapImageForGif("ceilingfanoff.gif");
            BitmapImage tvOffBitmapImage = getBitmapImageForGif("tvoff.gif");
            BitmapImage stereoOffBitmapImage = getBitmapImageForGif("stereooff.gif");
            BitmapImage hottubOffBitmapImage = getBitmapImageForGif("hottuboff.gif");
            BitmapImage closedGarageBitmapImage = getBitmapImageForGif("closegarage.gif");
            BitmapImage lightBulbOffBitmapImage = getBitmapImageForGif("lightbulboff.gif");
            BitmapImage livingRoomLightsOffBitmapImage = getBitmapImageForGif("livingroomlightsoff.gif");
            BitmapImage bathroomLightsOffBitmapImage = getBitmapImageForGif("bathroomlightsoff.gif");
            BitmapImage dvdOffBitmapImage = getBitmapImageForGif("dvdoff.gif");
            BitmapImage bathroomstereoOffBitmapImage = getBitmapImageForGif("stereooff.gif");


            ExecuteBitmapImageDictionary.Add("RemoteController.CeilingFanOnCommand", ceilingFanOnBitmapImage);
            ExecuteBitmapImageDictionary.Add("RemoteController.CeilingFanOffCommand", ceilingFanOffBitmapImage);
            ExecuteBitmapImageDictionary.Add("RemoteController.TVOnCommand", tvOnBitmapImage);
            ExecuteBitmapImageDictionary.Add("RemoteController.TVOffCommand", tvOffBitmapImage);
            ExecuteBitmapImageDictionary.Add("RemoteController.StereoOnCommand", stereoOnBitmapImage);
            ExecuteBitmapImageDictionary.Add("RemoteController.StereoOffCommand", stereoOffBitmapImage);
            ExecuteBitmapImageDictionary.Add("RemoteController.HottubOnCommand", hottubOnBitmapImage);
            ExecuteBitmapImageDictionary.Add("RemoteController.HottubOffCommand", hottubOffBitmapImage);
            ExecuteBitmapImageDictionary.Add("RemoteController.GarageDoorUpCommand", openGarageBitmapImage);
            ExecuteBitmapImageDictionary.Add("RemoteController.GarageDoorDownCommand", closedGarageBitmapImage);
            ExecuteBitmapImageDictionary.Add("RemoteController.LightOnCommand", lightBulbOnBitmapImage);
            ExecuteBitmapImageDictionary.Add("RemoteController.LightOffCommand", lightBulbOffBitmapImage);
            ExecuteBitmapImageDictionary.Add("RemoteController.LivingroomLightOnCommand", livingRoomLightsOnBitmapImage);
            ExecuteBitmapImageDictionary.Add("RemoteController.LivingroomLightOffCommand", livingRoomLightsOffBitmapImage);
            ExecuteBitmapImageDictionary.Add("RemoteController.BathroomLightOnCommand", bathroomLightsOnBitmapImage);
            ExecuteBitmapImageDictionary.Add("RemoteController.BathroomLightOffCommand", bathroomLightsOffBitmapImage);
            ExecuteBitmapImageDictionary.Add("RemoteController.DVDOnCommand", dvdOnBitmapImage);
            ExecuteBitmapImageDictionary.Add("RemoteController.DVDOffCommand", dvdOffBitmapImage);
            ExecuteBitmapImageDictionary.Add("RemoteController.StereOnCommand", bathroomstereoOnBitmapImage);
            ExecuteBitmapImageDictionary.Add("RemoteController.StereOffCommand", bathroomstereoOffBitmapImage);


            UndoBitmapImageDictionary.Add("RemoteController.CeilingFanOnCommand", ceilingFanOffBitmapImage);
            UndoBitmapImageDictionary.Add("RemoteController.CeilingFanOffCommand", ceilingFanOnBitmapImage);
            UndoBitmapImageDictionary.Add("RemoteController.TVOnCommand", tvOffBitmapImage);
            UndoBitmapImageDictionary.Add("RemoteController.TVOffCommand", tvOnBitmapImage);
            UndoBitmapImageDictionary.Add("RemoteController.StereoOnCommand", stereoOffBitmapImage);
            UndoBitmapImageDictionary.Add("RemoteController.StereoOffCommand", stereoOnBitmapImage);
            UndoBitmapImageDictionary.Add("RemoteController.HottubOnCommand", hottubOffBitmapImage);
            UndoBitmapImageDictionary.Add("RemoteController.HottubOffCommand", hottubOnBitmapImage);
            UndoBitmapImageDictionary.Add("RemoteController.GarageDoorUpCommand", closedGarageBitmapImage);
            UndoBitmapImageDictionary.Add("RemoteController.GarageDoorDownCommand", openGarageBitmapImage);
            UndoBitmapImageDictionary.Add("RemoteController.LightOnCommand", lightBulbOffBitmapImage);
            UndoBitmapImageDictionary.Add("RemoteController.LightOffCommand", lightBulbOnBitmapImage);
            UndoBitmapImageDictionary.Add("RemoteController.LivingroomLightOnCommand", livingRoomLightsOffBitmapImage);
            UndoBitmapImageDictionary.Add("RemoteController.LivingroomLightOffCommand", livingRoomLightsOnBitmapImage);
            UndoBitmapImageDictionary.Add("RemoteController.BathroomLightOnCommand", bathroomLightsOffBitmapImage);
            UndoBitmapImageDictionary.Add("RemoteController.BathroomLightOffCommand", bathroomLightsOnBitmapImage);
            UndoBitmapImageDictionary.Add("RemoteController.DVDOnCommand", dvdOffBitmapImage);
            UndoBitmapImageDictionary.Add("RemoteController.DVDOffCommand", dvdOnBitmapImage);
            UndoBitmapImageDictionary.Add("RemoteController.StereOnCommand", bathroomstereoOffBitmapImage);
            UndoBitmapImageDictionary.Add("RemoteController.StereOffCommand", bathroomstereoOnBitmapImage);
        }

        private static BitmapImage getBitmapImageForGif(string imageName)
        {
            BitmapImage myBitmapImage = new BitmapImage();

            myBitmapImage.BeginInit();
            myBitmapImage.UriSource = new Uri("pack://application:,,/" + imageName);
            myBitmapImage.DecodePixelWidth = 200;
            myBitmapImage.EndInit();

            return myBitmapImage;
        }

        #endregion

        // constructor
        public GUICommandDecorator(Command command, GUICommandDecoratorStrategy GUIStrategy)
        {
            this.GUIStrategy = GUIStrategy;
            this.command = command;
        }

        public override object Execute()
        {
            BitmapImage ExecuteBitmapImage = null;

            if(ExecuteBitmapImageDictionary.TryGetValue(command.ToString(), out ExecuteBitmapImage)) ///emulate the retrieval of a picture of the object's status
                GUIStrategy.ShowGUIExecute(ExecuteBitmapImage);

            return command.Execute();
        }
        public override object Undo()
        {
            BitmapImage UndoBitmapImage = null;

            if (UndoBitmapImageDictionary.TryGetValue(command.ToString(), out UndoBitmapImage))  ///emulate the retrieval of a picture of the object's status
                GUIStrategy.ShowGUIUndo(UndoBitmapImage);

            return command.Undo();
        }
    }
}
